package com.msh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import com.msh.dao.SkuClassDao;
import com.msh.dao.SkuDao;
import com.msh.entity.SkuClassEntity;
import com.msh.entity.SkuEntity;
import com.msh.exception.LogicException;
import com.msh.service.SkuClassService;
import com.msh.viewmodel.Pager;
import org.apache.ibatis.logging.LogException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SkuClassServiceImpl extends ServiceImpl<SkuClassDao,SkuClassEntity> implements SkuClassService{
    @Autowired
    private SkuDao skuDao;

    @Override
    public Pager<SkuClassEntity> findPage(long pageIndex, long pageSize, String className) {
        var page = new Page<SkuClassEntity>(pageIndex,pageSize);
        if(Strings.isNullOrEmpty(className)){
            this.page(page);
        }else {
            var qw = new LambdaQueryWrapper<SkuClassEntity>();
            qw.like(SkuClassEntity::getClassName,className);

            this.page(page,qw);
        }

        return Pager.build(page);
    }

    @Override
    public Boolean removeClass(Integer classId) {
        if (classId == null) throw new LogException("请输入需要删除的商品分类id");
        if (skuDao.countByClassId(classId) > 0) throw new LogicException("当前商品分类下存在使用的商品");
        this.removeById(classId);
        return true;
    }
}
